package com.goldsprite.ruletilemap.gdxexamples;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;
import com.goldsprite.ruletilemap.core.BaseMapRenderer;

import java.util.LinkedHashMap;
import java.util.Map;

public class GdxMapRenderer extends BaseMapRenderer {
	private SpriteBatch batch;
	private LinkedHashMap<String, TextureRegion> textureMap;
	private Texture blackPlaceholder;

	public GdxMapRenderer(SpriteBatch batch) {
		this.batch = batch;
		this.textureMap = new LinkedHashMap<>();
		this.tileSize = 32; // GDX渲染使用32像素
		createBlackPlaceholder();
	}

	@Override
	public void initializeIcons(Map<String, Object> iconMap) {
		textureMap.clear();
		for (Map.Entry<String, Object> entry : iconMap.entrySet()) {
			if (entry.getValue() instanceof TextureRegion) {
				textureMap.put(entry.getKey(), (TextureRegion) entry.getValue());
			}
		}
		// 确保未确定图块使用黑色占位
		textureMap.put("g1", new TextureRegion(blackPlaceholder));
	}

	public void initializeWithGrassTexture(Texture grassTexture) {
		Array<TextureRegion> regions = TextureRegionCutter.cutTexture(grassTexture, 16, 3, 3);

		Map<String, Object> iconMap = new LinkedHashMap<>();
		// 映射ID到纹理区域
		String[] tileIds = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
		for (int i = 0; i < Math.min(regions.size, tileIds.length); i++) {
			iconMap.put(tileIds[i], regions.get(i));
		}
		// 空气使用透明（不绘制）
		iconMap.put("0", null);
		// 未确定图块使用黑色占位
		iconMap.put("g1", blackPlaceholder);

		initializeIcons(iconMap);
	}
	public void initializeWithTextureAtlas(Texture tex, int uvUnit) {
		// 计算纹理网格尺寸
		int cols = tex.getWidth() / uvUnit;
		int rows = tex.getHeight() / uvUnit;
		int totalTiles = cols * rows;

		// 切割纹理
		Array<TextureRegion> regions = TextureRegionCutter.cutTexture(tex, uvUnit, rows, cols);

		Map<String, Object> iconMap = new LinkedHashMap<>();

		// 自动生成ID列表：从1开始到totalTiles
		for (int i = 0; i < regions.size; i++) {
			String tileId = String.valueOf(i + 1); // ID从1开始
			iconMap.put(tileId, regions.get(i));
		}

		// 空气使用透明（不绘制）
		iconMap.put("0", null);
		// 未确定图块使用黑色占位
		iconMap.put("g1", blackPlaceholder);

		initializeIcons(iconMap);
	}

	@Override
	public void renderMap(String mapData) {
		String[][] grid = parseMapToGrid(mapData);
		int gridHeight = grid.length;
		int gridWidth = grid[0].length;

		// 计算居中位置
		float startX = (GdxLauncher.getWorldWidth() - gridWidth * tileSize) / 2f;
		float startY = (GdxLauncher.getWorldHeight() - gridHeight * tileSize) / 2f;

		batch.begin();
		for (int y = 0; y < gridHeight; y++) {
			for (int x = 0; x < gridWidth; x++) {
				String tileId = grid[y][x];
				TextureRegion region = textureMap.get(tileId);

				if (region != null) {
					float drawX = startX + x * tileSize;
					float drawY = startY + (gridHeight - 1 - y) * tileSize; // 反转Y轴
					batch.draw(region, drawX, drawY, tileSize, tileSize);
				}
			}
		}
		batch.end();
	}

	@Override
	public void dispose() {
		if (blackPlaceholder != null) {
			blackPlaceholder.dispose();
			blackPlaceholder = null;
		}
		textureMap.clear();
	}

	private void createBlackPlaceholder() {
		Pixmap pixmap = new Pixmap(tileSize, tileSize, Pixmap.Format.RGBA8888);
		pixmap.setColor(Color.BLACK);
		pixmap.fill();
		blackPlaceholder = new Texture(pixmap);
		pixmap.dispose();
	}
}
